<?php

class IzwebzCore {

	/**
    * The IzwebzCore Version number.
    * @type string
    */
    public $Version = '1.0';

    /**
    * Global Database WordPress
    * @type object
    */
    public $wpdb;

    /**
    * Table Prefix
    * @type string
    */
    public $corePrefix = 'izCore_';

    /**
    * Global Current Users
    * @type object
    */
    public $current_user;

    /**
    * Table Public in core
    * @type string
    */
    public $FBLogin = '';
    public $PracticeTest = '';
    public $PracticeTestAnswers = '';
    public $PracticeTestQuestions = '';
    public $PracticeTestResults = '';
    public $PracticeTestRanks = '';
    public $PracticeTestTemporaryResults = '';

    public static $api = array();

    function __construct()
    {
    	global $wpdb, $current_user;
    	$this->wpdb = $wpdb;
    	$this->current_user = $current_user;
    	$this->FBLogin = $this->corePrefix."FBLogin";
    	$this->PracticeTest = $this->corePrefix."PracticeTest";
    	$this->PracticeTestAnswers = $this->corePrefix."PracticeTestAnswers";
    	$this->PracticeTestQuestions = $this->corePrefix."PracticeTestQuestions";
    	$this->PracticeTestResults = $this->corePrefix."PracticeTestResults";
    	$this->PracticeTestRanks = $this->corePrefix."PracticeTestRanks";
    	$this->PracticeTestTemporaryResults = $this->corePrefix."PracticeTestTemporaryResults";
    }

    public static function autoloadControllers()
	{
		$files = scandir(CORE_DIR.'controllers',1);
		foreach ($files as $file) {
			if($file == '.' || $file == '..' || $file == '.svn') continue;
			require_once(CORE_DIR.'controllers/'.$file);
		}
	}

	public static function listAPI()
	{
		$files = scandir(CORE_DIR.'api',1);
		foreach ($files as $file) {
			if($file == '.' || $file == '..' || $file == '.svn' || $file == 'API.php') continue;
			self::$api[] = str_replace(".php","",$file);
		}
		return self::$api;
	}

	public static function autoloadAPIs()
	{
		$files = scandir(CORE_DIR.'api',1);
		foreach ($files as $file) {
			if($file == '.' || $file == '..' || $file == '.svn') continue;
			require_once(CORE_DIR.'api/'.$file);
		}
	}

	public static function loadAPI()
	{
		if(isset($_GET['api']) && $_GET['api']=="izwebz_api"){
			if(in_array($_GET['api_name'],self::listAPI()) && class_exists($_GET['api_name'])){
				new $_GET['api_name'];
			}else{
				echo json_encode(array('status'=>500,'error'=>true,'message'=>'The server encountered an error processing your request'));
			}
		}
	}

	public function loadFunctionOutSite()
	{
		$webroot = $_SERVER['DOCUMENT_ROOT'];
		require_once($webroot . '/wp-blog-header.php');
		require_once($webroot . '/wp-config.php');
		require_once($webroot . '/wp-load.php');
		require_once($webroot . '/wp-includes/pluggable.php');
	}

	public function loadModel($modelClass)
	{
		if(!empty($modelClass)) {
			if(is_array($modelClass)){
				foreach ($modelClass as $model) {
					$fileModel = CORE_DIR.'models/'.$model.'.php';
					if(is_file($fileModel) && file_exists($fileModel)) {
						require_once($fileModel);
						$this->$model = new $model;
					}
				}
			}else{
				$fileModel = CORE_DIR.'models/'.$modelClass.'.php';
				if(is_file($fileModel) && file_exists($fileModel)) {
					require_once($fileModel);
					return $this->$modelClass = new $modelClass;
				}else{
					return null;
				}
			}
		}
	}

	public function loadComponent($componentClass)
	{
		if(!empty($componentClass)) {
			if(is_array($componentClass)){
				foreach ($componentClass as $component) {
					$fileComponent = CORE_DIR.'components/'.$component.'.php';
					if(is_file($fileComponent) && file_exists($fileComponent)) {
						require_once($fileComponent);
						$this->$component = new $component;
					}
				}
			}else{
				$fileComponent = CORE_DIR.'components/'.$componentClass.'.php';
				if(is_file($fileComponent) && file_exists($fileComponent)) {
					require_once($fileComponent);
					return $this->$componentClass = new $componentClass;
				}else{
					return null;
				}
			}
		}
	}
	
	public function loadHelper($helperClass)
	{
		if(!empty($helperClass)) {
			if(is_array($helperClass)){
				foreach ($helperClass as $helper) {
					$fileHelper = CORE_DIR.'helpers/'.$helper.'.php';
					if(is_file($fileHelper) && file_exists($fileHelper)) {
						require_once($fileHelper);
						$this->$helper = new $helper;
					}
				}
			}else{
				$fileHelper = CORE_DIR.'helpers/'.$helperClass.'.php';
				if(is_file($fileHelper) && file_exists($fileHelper)) {
					require_once($fileHelper);
					return $this->$helperClass = new $helperClass;
				}else{
					return null;
				}
			}
		}
	}
}